package java_thinking.unit_13._13_5;

import java_thinking.net.mindview.util.BinaryFile;

import java.io.File;
import java.io.IOException;

/**
 * @Description : 一个十六进制转储工具
 * @author: cww
 * @DateTime: 2019-03-24 22:54
 */

public class Hex {
    public static String format(byte[] data) {
        StringBuilder builder = new StringBuilder();
        int n = 0;
        for (byte b : data){
            if (n % 16 == 0){
                builder.append(String.format("%05X: ",n));
            }
            builder.append(String.format("%02X ",b));
            n++;
            if (n%16==0){
                builder.append("\n");
            }
        }
        builder.append("\n");
        return builder.toString();
    }

    public static void main(String[] args) {
        try {
            if (args.length == 0){
                System.out.println(format(BinaryFile.read("Hex.class")));
            } else {
                System.out.println(format(BinaryFile.read(new File(args[0]))));
            }
        } catch (IOException e) {
            System.out.println(e);
        }
    }
}
